<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/28
 * Time: 10:35
 */

namespace app\sports_admin\model;
use think\Model;
use think\Request;
class TrainLesson extends Model
{
    protected $table = "train_lesson";
    protected $pk    = "id";
    //课程列表
    public static function trainLesson($page,$rows,$sort,$sortOrder,$searchName,$trainId)
    {
        $adminList = self::where(function($query)use($searchName){
            if($searchName){
                $query->where('title','like',"%{$searchName}%");
            }})
            ->where('train_id',$trainId)
            ->page($page)
            ->limit($rows)
            ->order("{$sort}","{$sortOrder}")->select();

        $adminCount = self::where(function($query)use($searchName){
            if($searchName){
                $query->where('title','like',"%{$searchName}%");
            }})
            ->where('train_id',$trainId)
            ->count();
        return ['rows'=>$adminList,'total'=>$adminCount];
    }
    //删除课程
    public static function trainLessonDel()
    {
        $data = Request::instance()->only("id");
        return self::where('id',$data['id'])->delete();
    }
    //添加课程信息
    public static function trainLessonAdd()
    {
        $data = Request::instance()->only("train_id,title,content,age_min,age_max,photo");
        $content = htmlspecialchars($data['content']);
        $age  = $data['age_min']."-".$data['age_max'];
        $create_time = time();
        $update_time = time();
        return self::create(["train_id"=>$data['train_id'],"title"=>$data['title'],"content"=>$content,"photo"=>$data['photo'],
            "age_min"=>$data['age_min'],"age_max"=>$data['age_max'],"age"=>$age,"create_time"=>$create_time,"update_time"=>$update_time]);
    }

}